<?php
/**
 * 
 * @author jason
 *
 */
class BlogApi extends Api{

	/**
	 * 知识详情 --using
	 * @param integer id 帖子ID
	 * @return array 帖子信息
	 */
	function post_detail(){
		$this->user_id = empty($this->user_id) ? $this->mid : $this->user_id;
		$data = D('blog')->where('id='.$this->id)->find();
		$data['user_info'] = $this->get_user_info( $data['uid'] );
		if(empty($data['from'])){
			$data['from'] = '来自网站';
		}
		if($data['from']==1){
			$data['from'] = '来自手机网页版';
		}
		if($data['from']==2){
			$data['from'] = '来自android';
		}
		if($data['from']==3){
			$data['from'] = '来自iphone';
		}
		if(D('blog_favorite')->where('blog_id='.$this->id.' AND uid='.$this->user_id)->find()){
			$data['is_favorite'] = 1;
		}else{
			$data['is_favorite'] = 0;
		}
		$is_digg = M('blog_digg')->where('post_id='.$this->id.' and uid='.$this->mid)->find();
		$data['is_digg']= $is_digg ? '1':'0';
		$data['comment_info'] = $this->blog_comments($data['feed_id'],10);
		$data['digg_info'] = $this->blog_digg($this->id);
		if($data){
			return $data;
		}else{
			return array();
		}
	}
	
	/**
	 * 知识分类 --using
	 * @param integer id 知识ID
	 * @return array 知识信息
	 */
	function blog_category(){
			//先从缓存里面获取
		$result = M('blog_category')->where("`uid`=0".' and pid=0')->field( 'name,uid,id,pid' )->findAll();
			//重组数据集结构
		$newresult = array();
		foreach ( $result as $value ) {
				$newresult[$value['id']]['id']   = $value['id'];
				$newresult[$value['id']]['name'] = $value['name'];
				$newresult[$value['id']]['uid']  = $value['uid'];
		}
		
		return array_values($newresult);		
	}
	
	
	function blog_list(){
		$order = 'id desc';
		if($_REQUEST['category']){
			$map['fcategory'] = intval($_REQUEST['category']);
		}
		if($_REQUEST['key']){
			$map['title'] = array('like','%'.$_REQUEST['key'].'%');
		}
		$max_id = $this->max_id ? intval($this->max_id) : 0;
		$count = $this->count ? intval($this->count) : 20;
		!empty($max_id) && $map['id'] = array('lt', $max_id);
		$uid = $this->mid;
		$map['_string'] = ' private = 0 ';
		// 获取博主关注的UID
		$blogAuthorUids = model('Follow')->where('uid='.$uid)->field('fid')->findAll();
		$blogAuthorUids = getSubByKey($blogAuthorUids, 'fid');
			if(!empty($blogAuthorUids)) {
				$authorMap = implode(',', $blogAuthorUids);
		
				if(!empty($map['_string'])) {
					$map['_string'] .= ' OR (uid IN ('.$authorMap.') AND private = 2)';
				} else {
					$map['_string'] .= '(uid IN ('.$authorMap.') AND private = 2)';
				}
			}
		
			// 仅仅对自己可见
			if(!empty($map['_string'])) {
				$map['_string'] .= ' OR (uid = '.$uid.' AND private = 4)';
			} else {
				$map['_string'] .= ' (uid = '.$uid.' AND private = 4)';
			}
			// 仅对粉丝可见
		
			if(!empty($map['_string'])) {
				$map['_string'] .= ' OR uid='.$uid;
			} else {
				$map['_string'] .= ' uid='.$uid;
		    }
		$list = D('blog')->where($map)->order($order)->limit($count)->findAll();
		if(!$list){
			$list = array();
		}else{
			foreach ($list as $k=>$v){
				$list[$k]['content'] = getShort(t($v['content']),50);
			}
		}
		return $list;
	}
    //我的知识
	function blog_list_my(){
		$order = 'id desc';
		if($_REQUEST['category']){
			$map['fcategory'] = intval($_REQUEST['category']);
		}
		$map['uid'] = empty($this->user_id) ? $this->mid : $this->user_id;
		if($_REQUEST['key']){
			$map['title'] = array('like','%'.$_REQUEST['key'].'%');
		}
		$map['status'] = 1;
		$max_id = $this->max_id ? intval($this->max_id) : 0;
		$count = $this->count ? intval($this->count) : 20;
		!empty($max_id) && $map['id'] = array('lt', $max_id);
		$list = D('blog')->where($map)->order($order)->limit($count)->findAll();
		if(!$list){
			$list = array();
		}else{
			foreach ($list as $k=>$v){
				$list[$k]['content'] = getShort(t($v['content']),50);
			}
		}
		return $list;
	}
	
	//赞帖子
	public function addDigg(){
		$maps['post_id'] = $map['post_id'] = intval($_REQUEST['row_id']);
		$map['uid'] = empty($this->user_id) ? $this->mid : $this->user_id;
		$hasdigg = M('blog_digg')->where($map)->find();
		$map['cTime'] = time();
		if(!$hasdigg){
			$result = M('blog_digg')->add($map);
			if($result){
				$post = M('blog')->where($maps)->find();
				M('blog')->where($maps)->setField('praise',$post['praise']+1);
				$res['status'] = 1;
				$res['info'] = '赞成功';
			}else{
				$res['status'] = 0;
				$res['info'] = '赞失败';
			}
		}else{
			$res['status'] = 0;
			$res['info'] = '您以赞过';
		}
		return $res;
	}
	//取消赞帖子
	public function delDigg () {
		$maps['post_id'] = $map['post_id'] = intval($_REQUEST['row_id']);
		$map['uid'] = empty($this->user_id) ? $this->mid : $this->user_id;
		$hasdigg = M('blog_digg')->where($map)->find();
		if ($hasdigg) {
			if(M('blog_digg')->where($map)->delete()){
				$post = M('blog')->where($maps)->find();
				M('blog')->where($maps)->setField('praise',$post['praise']-1);
				$res['status'] = 1;
				$res['info'] = '取消赞成功';
			}else{
				$res['status'] = 0;
				$res['info'] = '取消赞失败';
			}
		} else {
			$res['status'] = 0;
			$res['info'] = '您还没赞过';
		}
		return $res;
	}
	
	/**
	 * 收藏帖子
	 * @return void
	 */
	public function favorite(){
		$data['post_id'] = intval($_REQUEST['post_id']);
		$data['weiba_id'] = intval($_REQUEST['weiba_id']);
		$data['post_uid'] = intval($_REQUEST['post_uid']);
		$data['uid'] = empty($this->user_id) ? $this->mid : $this->user_id;
		$resault = M('blog_favorite')->where($data)->find();
		$data['favorite_time'] = time();
        if(!$resault){
        	if(M('blog_favorite')->add($data)){
        		D('UserData')->updateKey('collect_topic_count',1);
        		D('UserData')->updateKey('collect_total_count',1);
        	
        		//添加积分
        		model('Credit')->setUserCredit($this->mid,'collect_topic');
        		model('Credit')->setUserCredit($data['post_uid'],'collected_topic');
        	
        		$res['status']  = 1;
        		$res['msg']  = '收藏成功';
        	}else{
        		$res['status']  = 0;
        		$res['msg']  = '收藏失败';
        	}
        }else{
        	$res['status']  = 0;
        	$res['msg']  = '您已经收藏过';
        }
		return $res;
	}
	
	/**
	 * 取消收藏帖子
	 * @return void
	 */
	public function unfavorite(){
		$map['post_id'] = intval($_REQUEST['post_id']);
		$map['uid'] = empty($this->user_id) ? $this->mid : $this->user_id;
		$resault = M('blog_favorite')->where($map)->find();
		if($resault){
			if(M('blog_favorite')->where($map)->delete()){
				D('UserData')->updateKey('collect_topic_count',-1);
				D('UserData')->updateKey('collect_total_count',-1);
				$res['status']  = 1;
				$res['msg']  = '取消收藏成功';
			}else{
				$res['status']  = 0;
				$res['msg']  = '取消收藏失败';
			}
		}else{
			$res['status']  = 0;
			$res['msg']  = '你还没有收藏';
		}
		return $res;
	}
	
	/**
	 * 获取指定分享的评论列表 --using
	 * @param integer feed_id 分享ID
	 * @param integer max_id 上次返回的最后一条评论ID
	 * @param integer count 评论条数
	 * @return array 评论列表
	 */
	public function blog_comments($feed_id,$count){
		if(!$feed_id){
			$feed_id = $this->data['feed_id'];
		}
		$comment_list = array();
		$where = 'is_del=0 and row_id='.$feed_id;
		if(!$count){
			$count = $this->count;
			!empty($this->max_id) && $where .= " AND comment_id < {$this->max_id}";
		}
		$floor = '';
		if($this->max_id){
			$floor = M('comment')->where( 'is_del=0 and row_id='.$feed_id.' and comment_id <'.$this->max_id )->count();
		}else{
			$floor = M('comment')->where( 'is_del=0 and row_id='.$feed_id )->count();
		}
		$comments = model('Comment')->where($where)->order('comment_id DESC')->limit($count)->findAll();
		foreach($comments as $v) {
			switch ($v['type']) {
				case '2':
					$type = '转发了此贴';
					break;
				case '3':
					$type = '分享了此贴';
					break;
				case '4':
					$type = '赞了此贴';
					break;
				default:
					$type = '评论了此贴';
					break;
			}
			$comment_info['type'] = $type;
			$comment_info['floor'] = $floor;
			$floor--;
			$comment_info['user_info'] = $this->get_user_info($v['uid']);
			$comment_info['comment_id'] = $v['comment_id'];
			$comment_info['content'] = $v['content'];
			$comment_info['ctime'] = $v['ctime'];
			$comment_info['digg_count'] = $v['digg_count'];
			$diggarr = model('CommentDigg')->checkIsDigg($v['comment_id'], $GLOBALS['ts']['mid']);
			$comment_info['is_digg'] = t($diggarr[$v['comment_id']] ? 1 : 0);
			$comment_list[] = $comment_info;
		}
		return $comment_list;
	}
	
	/**
	 * 获取指定帖子的赞过的人的列表 --using
	 * @param integer feed_id 分享ID
	 * @param integer max_id 上次返回的最后一条赞的ID
	 * @param integer count 数量
	 * @return array 点赞的用户列表
	 */
	public function blog_digg($feed_id,$count=10){
		if(!$feed_id){
			$feed_id = $this->id;
		}
		$where = 'post_id='.$feed_id;
		!empty($this->max_id) && $where .= " AND id < {$this->max_id}";
		$digg_list = M( 'blog_digg' )->where($where)->order('cTime DESC')->limit($count)->findAll();
		if(!$digg_list){
			return array();
		}
		$follow_status = model('Follow')->getFollowStateByFids($this->mid,getSubByKey($digg_list,'uid'));
		foreach ($digg_list as $k => $v) {
			$user_info = api('User')->get_user_info($v['uid']);
			$digg_list[$k]['uname'] = $user_info['uname'];
			$digg_list[$k]['intro'] = $user_info['intro'];
			$digg_list[$k]['avatar'] = $user_info['avatar']["avatar_middle"];
			$digg_list[$k]['follow_status'] = $follow_status[$v['uid']];
			unset($digg_list[$k]['post_id']);
		}
		return $digg_list;
	}
	
	/**
	 * 获取用户信息 --using
	 * @param integer uid 用户UID
	 * @return array 用户信息
	 */
	private function get_user_info($uid){
		$user_info_whole = api('User')->get_user_info($uid);
		$user_info['uid'] = $user_info_whole['uid'];
		$user_info['uname'] = $user_info_whole['uname'];
		$user_info['avatar']['avatar_middle'] = $user_info_whole['avatar']['avatar_middle'];
		$user_info['user_group'] = $user_info_whole['user_group'];
		return $user_info;
	}
}